package gewu.suleju.controller;


import gewu.suleju.model.pojo.User;
import gewu.suleju.model.pojo.UserDetail;
import gewu.suleju.model.vo.Result;
import gewu.suleju.model.vo.UpdateUser;
import gewu.suleju.sevice.imp.IUserDetailServiceImp;
import gewu.suleju.sevice.imp.UserServiceImp;
import gewu.suleju.util.JWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

@Controller
@RequestMapping("UserDetails")
public class UserDetailsController {
    @Autowired
    private UserServiceImp userServiceImp;
    @ResponseBody
    @PostMapping("findUserByUserId")
    public Result findUserByUserId(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader("token");
        Claims claims = JWTUtil.checkJWT(token);
        String username=(String)claims.get("username");
        User user=userServiceImp.findUserByUsername(username);
        return new Result("200","查询用户成功",user);

    }

    @Autowired
    private IUserDetailServiceImp iUserDetailServiceImp;
    @ResponseBody
    @PostMapping("findUserDetailByUserId")
    public Result findUserDetailByUserId(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader("token");
        Claims claims = JWTUtil.checkJWT(token);
        Integer userid=(Integer)claims.get("id");
        UserDetail userDetail=iUserDetailServiceImp.findUserDetailByUserId(userid);
        return new Result("200","查询用户详情成功",userDetail);
    }

    @Autowired
    private IUserDetailServiceImp iUserDetailServiceImp2;
    @ResponseBody
    @GetMapping("newUser")
    public Result  newUser (String username,String password,String phone,String email,String sex,String birthday ,String hometown,String profession,String tag,String person_describe,String company, HttpServletRequest request,HttpServletResponse response) {
        String token = request.getHeader("token");
        Claims claims = JWTUtil.checkJWT(token);
        Integer id = (Integer)claims.get("id");
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        Date date=format.parse(birthday);
        //String username,String password,String phone,String email,Integer id
        iUserDetailServiceImp2.updateUserDeAll(username,password,phone,email,id);
        //String sex,String birthday ,String hometown,String profession,String tag,String person_describe,String company,Integer id
        iUserDetailServiceImp2.updateUserDetail(sex,birthday ,hometown, profession,tag,person_describe, company,id);
        User user2=new User();
        user2.setId(id);
        user2.setUsername(username);
        user2.setEmail(email);
        user2.setPassword(password);
        user2.setPhone(phone);
        user2.setHeadPicAddress("-1");
        user2.setIsStatus(1);
        token=JWTUtil.geneJsonWebToken(user2);
        return new Result("200","成功",token);
    }
}
